package com.mes.biz.service.impl;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizProcessPlan;
import com.mes.biz.domain.BizProductProcessDetail;
import com.mes.biz.domain.BizProductionProcess;
import com.mes.biz.mapper.BizProductProcessDetailMapper;
import com.mes.biz.service.BizProductProcessDetailService;
import com.mes.biz.service.BizProductionProcessService;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 工序计划明细Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-05-03
 */
@Service
@Transactional(readOnly = true)
public class BizProductProcessDetailServiceImpl extends BaseServiceImpl<BizProductProcessDetailMapper, BizProductProcessDetail> implements BizProductProcessDetailService {

    private static final Logger log = LoggerFactory.getLogger(BizProductProcessDetailServiceImpl.class);
    @Resource
    private BizProductionProcessService bizProductionProcessService;
    @Resource
    private BizProductProcessDetailMapper bizProductProcessDetailMapper;
    /**
     * 获取单条数据
     * @param bizProductProcessDetail 工序计划明细
     * @return 工序计划明细
     */
    @Override
    public BizProductProcessDetail get(BizProductProcessDetail bizProductProcessDetail) {
        BizProductProcessDetail dto = super.get(bizProductProcessDetail);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 工序计划明细id
     * @return 工序计划明细
     */
    @Override
    public BizProductProcessDetail get(String id) {
        BizProductProcessDetail dto = super.get(id);
        return dto;
    }

    /**
     * 查询工序计划明细列表
     * @param bizProductProcessDetail 工序计划明细
     * @return 工序计划明细
     */
    @Override
    public List<BizProductProcessDetail> findList(BizProductProcessDetail bizProductProcessDetail) {
		List<BizProductProcessDetail> bizProductProcessDetailList = super.findList(bizProductProcessDetail);
        return bizProductProcessDetailList;
    }

    /**
     * 分页查询工序计划明细列表
     * @param bizProductProcessDetail 工序计划明细
     * @return 工序计划明细
     */
    @Override
    public PageInfo<BizProductProcessDetail> findPage(BizProductProcessDetail bizProductProcessDetail) {
		PageInfo<BizProductProcessDetail> page = super.findPage(bizProductProcessDetail);
        return page;
    }

    /**
     * 保存工序计划明细
     * @param bizProductProcessDetail
     * @return 结果
     */
    @Override
    public boolean save(BizProductProcessDetail bizProductProcessDetail) {
        return super.save(bizProductProcessDetail);
    }

    /**
     * 删除工序计划明细信息
     * @param bizProductProcessDetail
     * @return 结果
     */
    @Override
    public boolean remove(BizProductProcessDetail bizProductProcessDetail) {
        return super.remove(bizProductProcessDetail);
    }

    /**
     * 批量删除工序计划明细
     * @param ids 需要删除的工序计划明细ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBizProductProcessDetailByIds(String[] ids) {
        return mapper.deleteBizProductProcessDetailByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Override
    public List<BizProductProcessDetail> listByPlan(BizProductProcessDetail bizProductProcessDetail) {
        return mapper.findList(bizProductProcessDetail);
    }

    @Override
    @Transactional
    public boolean addDetail(List<BizProductProcessDetail> bizProductProcessDetails) {
        //查询工序计划
        String processPlanId = bizProductProcessDetails.get(0).getProductProcessId();
        //删除工序计划明细
        mapper.deleteByProcessPlanId(processPlanId,BaseEntity.DEL_FLAG_DELETE);
        //保存工序计划明细
        for(BizProductProcessDetail detail:bizProductProcessDetails) {
            BizProductionProcess bizProductionProcess =  bizProductionProcessService.get(detail.getProductionProcessId());
            detail.preInsert();
            detail.setTenantId(SecurityUtils.getTenantId());
            detail.setUnitProcessVol(new BigDecimal(1));
            detail.setUnitSalary(bizProductionProcess.getUnitSalary());
        }
        int result = mapper.saveBatch(bizProductProcessDetails);
        return result>0;
    }

    @Override
    public void saveBatch(List<BizProductProcessDetail> bizProductProcessDetailList) {
        //查询工序计划
        String processPlanId = bizProductProcessDetailList.get(0).getProductProcessId();
        //删除工序计划明细
        mapper.deleteByProcessPlanId(processPlanId,BaseEntity.DEL_FLAG_DELETE);
        mapper.saveBatch(bizProductProcessDetailList);
    }

    @Override
    public List<BizProductProcessDetail> getBizProductProcessDetailByProcessPlanId(String processPlanId) {
        return bizProductProcessDetailMapper.getBizProductProcessDetailByProcessPlanId(processPlanId);
    }

}
